home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / SHELLS / SZ2 / GEDITOR.IMP < prev    next >
Text File  |  1992-08-31  |  18KB  |  494 lines

  1.    {*******************************************************************
  2.  
  3.    GEDITOR.IMP
  4.  
  5.    *******************************************************************}
  6.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  7.  
  8.    EDITOR-SPECIFIC
  9.  
  10.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  11.    {===================================================================
  12.  
  13.    EXTENT - return "cascaded" extent of current or first TWindow
  14.  
  15.    ===================================================================}
  16. procedure FirstExtent ( VAR R : TRect ) ;
  17. begin
  18.    DeskTop^.GetExtent ( R ) ;
  19.    if Desktop^.Current = NIL then EXIT ;
  20.    if not Desktop^.Current^.GetState ( sfVisible ) then EXIT ;
  21.    Desktop^.Current^.GetBounds ( R ) ;
  22.    if R.B.X - R.A.X > MinWinSize.X then
  23.       inc ( R.A.X ) ;
  24.    if R.B.Y - R.A.Y > MinWinSize.Y then
  25.       inc ( R.A.Y ) ;
  26. end ;
  27.    {===================================================================
  28.  
  29.    EXTENT - return "cascaded" extent of current or first TEditWindow
  30.  
  31.    ===================================================================}
  32. procedure FirstExtentEd ( VAR R : TRect ) ;
  33.    {-------------------------------------------------------------------
  34.    CURRENT
  35.    -------------------------------------------------------------------}
  36. function Test ( P : PView ) : boolean ; FAR ;
  37. begin
  38.    Test                      := FALSE ;
  39.    if TypeOf ( P^ ) <> TypeOf ( TEditWindow ) then EXIT ;
  40.    with PEDITWINDOW ( P )^.Editor^ do
  41.    begin
  42.       if not P^.GetState ( sfVisible ) then EXIT ;
  43.       P^.GetBounds ( R ) ;
  44.       Test                   := TRUE ;
  45.       if R.B.X - R.A.X > MinWinSize.X then
  46.          inc ( R.A.X ) ;
  47.       if R.B.Y - R.A.Y > MinWinSize.Y then
  48.          inc ( R.A.Y ) ;
  49.    end ;
  50. end ;
  51.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  52.    PROCESS
  53.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  54. begin
  55.    DeskTop^.GetExtent ( R ) ;
  56.    Desktop^.FirstThat ( @Test ) ;
  57. end ;
  58.    {===================================================================
  59.  
  60.    NUMBER
  61.  
  62.    ===================================================================}
  63. function WinNumExist ( WinNum : integer ) : boolean ;
  64.    {-------------------------------------------------------------------
  65.    -------------------------------------------------------------------}
  66. function Test ( P : PView ) : boolean ; FAR ;
  67. begin
  68.    Test                      := FALSE ;
  69.    if not Selectable ( P ) then EXIT ;
  70.    Test                      := WinNum = PWINDOW ( P )^.Number ;
  71. end ;
  72.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  73.    PROCESS
  74.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  75. begin
  76.    WinNumExist               := Desktop^.FirstThat ( @Test ) <> NIL ;
  77. end ;
  78.    {===================================================================
  79.  
  80.    NUMBER
  81.  
  82.    ===================================================================}
  83. function NextWinNum : integer ;
  84. var
  85.    i                         : integer ;
  86. begin
  87.    for i := 1 to 9 do
  88.       if not WinNumExist ( i ) then
  89.       begin
  90.          NextWinNum          := i ;
  91.          EXIT ;
  92.       end ;
  93.    NextWinNum                := wnNoNumber ;
  94. end ;
  95.    {===================================================================
  96.  
  97.    Opens a window, with choice of whether visible.
  98.    Sets "AutoIndent" on.  If NewEdit, set wrap & right margin.
  99.  
  100.    ===================================================================}
  101. function OpenEditor ( FileName : FNameStr ;
  102.                       Visible : boolean ) : PEditWindow ;
  103. var
  104.    P                         : PView ;
  105.    R                         : TRect ;
  106. begin
  107.    OpenEditor                := NIL ;
  108.    if Visible then
  109.       FirstExtentEd ( R )
  110.    else
  111.       DeskTop^.GetExtent ( R ) ;
  112.    P                         := Application^.ValidView (
  113.                                 New ( PEditWindow ,
  114.                                 Init ( R , FileName, NextWinNum ) ) ) ;
  115.    if P = NIL then EXIT ;
  116.    with PEDITWINDOW ( P )^ do
  117.    begin
  118.       if not Visible then
  119.          Hide ;
  120.       Editor^.AutoIndent     := TRUE ;
  121. {$IFDEF newedit }
  122.       Editor^.Word_Wrap      := Default_Word_Wrap ;
  123.       Editor^.Right_Margin   := Default_Line_Length ;
  124. {$ENDIF }
  125.    end ;
  126.    DeskTop^.Insert ( P ) ;
  127.    OpenEditor                := PEDITWINDOW ( P ) ;
  128. end ;
  129.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  130.  
  131.    SAVE/CLOSE
  132.  
  133.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  134.    {===================================================================
  135.    IF CHANGED
  136.    ===================================================================}
  137. procedure SaveEdModified ;
  138. var
  139.    Count                     : byte ;
  140.    {-------------------------------------------------------------------
  141.    -------------------------------------------------------------------}
  142. procedure DoThis ( P : PView ) ; FAR ;
  143. begin
  144.    if TypeOf ( P^ ) <> TypeOf ( TEditWindow ) then EXIT ;
  145.    with PEDITWINDOW ( P )^.Editor^ do
  146.    begin
  147.       if not Modified then EXIT ;
  148.       SetBorder ( 14 ) ;                              { YELLOW - busy }
  149.       Save ;
  150.       inc ( Count ) ;
  151.    end ;
  152. end ;
  153.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  154.    PROCESS
  155.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  156. begin
  157.    Count                     := 0 ;
  158.    Desktop^.ForEach ( @DoThis ) ;
  159.    if Count > 0 then
  160.       SetBorder ( 0 ) ;                                { BLACK - done }
  161. end ;
  162.    {===================================================================
  163.  
  164.    ALWAYS A DIALOG FOR NAME
  165.  
  166.    ===================================================================}
  167. procedure SaveEdUntitled ;
  168.    {-------------------------------------------------------------------
  169.    -------------------------------------------------------------------}
  170. procedure DoThis ( P : PView ) ; FAR ;
  171. begin
  172.    if TypeOf ( P^ ) <> TypeOf ( TEditWindow ) then EXIT ;
  173.    with PEDITWINDOW ( P )^.Editor^ do
  174.    begin
  175.       if not Modified then EXIT ;
  176.       if FileName <> '' then EXIT ;
  177.       P^.MakeFirst ;                                 { bring to front }
  178.       SaveAs ;
  179.    end ;
  180. end ;
  181.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  182.    PROCESS
  183.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  184. begin
  185.    Desktop^.ForEach ( @DoThis ) ;
  186. end ;
  187.    {===================================================================
  188.  
  189.    CLOSE - "Untitled" TEditWindow(s).  Must force "Modified" flag,
  190.    since cmClose won't work if ^. Valid returns FALSE.
  191.  
  192.    ===================================================================}
  193. procedure CloseEdUntitled ;
  194.    {-------------------------------------------------------------------
  195.    -------------------------------------------------------------------}
  196. procedure DoThis ( P : PView ) ; FAR ;
  197. begin
  198.    if TypeOf ( P^ ) <> TypeOf ( TEditWindow ) then EXIT ;
  199.    with PEDITWINDOW ( P )^.Editor^ do
  200.    begin
  201.       if FileName <> '' then EXIT ;
  202.       Modified               := FALSE ;
  203.    end ;
  204.    Message ( P , evCommand , cmClose , NIL ) ;
  205. end ;
  206.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  207.    PROCESS
  208.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  209. begin
  210.    Desktop^.ForEach ( @DoThis ) ;
  211. end ;
  212.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  213.  
  214.    CLIP BOARD
  215.  
  216.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  217.    {===================================================================
  218.  
  219.    CREATE
  220.  
  221.    ===================================================================}
  222. procedure CreateClipboard ;
  223. var
  224.    R                         : TRect ;
  225. begin
  226.    if not BuffersInUse then EXIT ;
  227.    ClipWindow                := OpenEditor ( '' , FALSE ) ;
  228.    if ClipWindow = NIL then EXIT ;
  229.    Clipboard                 := ClipWindow^.Editor ;
  230.    Clipboard^.CanUndo        := FALSE ;
  231.    ClipWindow^.HelpCtx       := SaveClipCtx ;
  232.    ClipWindow^.Number        := wnNoNumber ;
  233.    Desktop^.GetExtent ( R ) ;
  234.    R.Grow ( -10 , -5 ) ;
  235.    ClipWindow^.Locate ( R ) ;
  236. end ;
  237.    {===================================================================
  238.  
  239.    DISPOSE
  240.  
  241.    ===================================================================}
  242. procedure DisposeClipboard ;
  243. begin
  244.    if ClipWindow = NIL then EXIT ;
  245.    SaveClipCtx               := ClipWindow^.HelpCtx ;
  246.    Dispose ( Clipboard , Done ) ;
  247.    Clipboard                 := NIL ;
  248.    Dispose ( ClipWindow , Done ) ;
  249.    ClipWindow                := NIL ;
  250. end ;
  251.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  252.  
  253.    BUFFERS - to start & end heap management
  254.  
  255.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  256.    {===================================================================
  257.  
  258.    PARAGRAPHS - Available heap space, in "Paragraphs"
  259.  
  260.    ===================================================================}
  261. function HeapParagraphs : word ;
  262. begin
  263.    HeapParagraphs            := PtrRec ( HeapEnd ).Seg -
  264.                                 PtrRec ( HeapPtr ).Seg ;
  265. end ;
  266.    {===================================================================
  267.  
  268.    BUFFER - Reserve "Paragraphs" for data, remaining for editor/buffers
  269.  
  270.    ===================================================================}
  271. procedure ReserveForData ( Paragraphs : word ) ;
  272. begin
  273.    if BufHeapSize <> 0 then EXIT ;
  274.    if HeapParagraphs > Paragraphs then
  275.       BufHeapSize            := HeapParagraphs - Paragraphs
  276.    else
  277.       BufHeapSize            := 0 ;
  278. end ;
  279.    {===================================================================
  280.  
  281.    HEAP - Reserve "Paragraphs" for editor/buffers, remaining for data
  282.  
  283.    ===================================================================}
  284. procedure ReserveForEditor ( Paragraphs : word ) ;
  285. begin
  286.    if BufHeapSize <> 0 then EXIT ;
  287.    if HeapParagraphs > Paragraphs then
  288.       BufHeapSize            := Paragraphs
  289.    else
  290.       BufHeapSize            := 0 ;
  291. end ;
  292.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  293.  
  294.    EDITOR
  295.  
  296.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  297.    {===================================================================
  298.  
  299.    TYPE
  300.  
  301.    ===================================================================}
  302. function IsEDIT ( P : PView ) : boolean ;
  303. begin
  304.    IsEDIT                    := ( P <> NIL ) and
  305.                                 ( TypeOf ( P^ ) = TypeOf ( TEditWindow ) ) ;
  306. end ;
  307.    {===================================================================
  308.  
  309.    EMPTY
  310.  
  311.    ===================================================================}
  312. function IsEditEmpty ( P : PView ) : boolean ;
  313. begin
  314.    IsEditEmpty               := IsEdit ( P ) and
  315.                                 ( PEDITWINDOW ( P )^.Editor^.BufLen = 0 ) ;
  316. end ;
  317.    {===================================================================
  318.  
  319.    SELECTED
  320.  
  321.    ===================================================================}
  322. function IsTextSelected ( P : PView ) : boolean ;
  323. begin
  324.    IsTextSelected            := IsEdit ( P ) and
  325.                                 ( PEDITWINDOW ( P )^.Editor^.SelStart <>
  326.                                   PEDITWINDOW ( P )^.Editor^.SelEnd ) and
  327.                                 (
  328.                                   ( PEDITWINDOW ( P )^.Editor^.CurPtr =
  329.                                     PEDITWINDOW ( P )^.Editor^.SelEnd ) or
  330.                                   ( PEDITWINDOW ( P )^.Editor^.CurPtr =
  331.                                     PEDITWINDOW ( P )^.Editor^.SelStart )
  332.                                 ) ;
  333. end ;
  334.    {===================================================================
  335.  
  336.    NAME - Return filename, since "title" may not contain path/dir.
  337.  
  338.    ===================================================================}
  339. function GetNameEdit ( P : PView ) : string ;
  340. begin
  341.    if IsEDIT ( P ) then
  342.       GetNameEdit            := PEDITWINDOW ( P )^.Editor^.FileName
  343.    else
  344.       GetNameEdit            := '' ;
  345. end ;
  346.    {===================================================================
  347.  
  348.    UNTITLED - if name blank & title = Untitled.
  349.  
  350.    ===================================================================}
  351. function IsUntitled ( P : PView ) : boolean ;
  352. begin
  353.    IsUntitled                := IsEdit ( P ) and
  354.                                 ( GetNameEdit ( P ) = '' ) and
  355.                                 ( Match ( 'untitled' ,
  356.                                           PWINDOW ( P )^.GetTitle ( 255 ) ) = 1 ) ;
  357. end ;
  358.    {===================================================================
  359.  
  360.    CLIPBOARD - if name blank & title = Clipboard.
  361.  
  362.    ===================================================================}
  363. function IsClipboard ( P : PView ) : boolean ;
  364. begin
  365.    IsClipboard               := IsEdit ( P ) and
  366.                                 not IsUntitled ( P ) and
  367.                                 ( GetNameEdit ( P ) = '' ) ;
  368. end ;
  369.    {===================================================================
  370.  
  371.    FILENAME - return pointer to first TEditWindow
  372.  
  373.    ===================================================================}
  374. function FirstEd : pointer ;
  375.    {-------------------------------------------------------------------
  376.    Test view
  377.    -------------------------------------------------------------------}
  378. function Test ( P : PView ) : boolean ; FAR ;
  379. begin
  380.    Test                      := IsEDIT ( P ) ;
  381. end ;
  382.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  383.    PROCESS
  384.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  385. begin
  386.    FirstEd                   := Desktop^.FirstThat ( @Test ) ;
  387. end ;
  388.    {===================================================================
  389.  
  390.    EDITOR
  391.  
  392.    ===================================================================}
  393. function ExistEditor : boolean ;
  394.    {-------------------------------------------------------------------
  395.    Any TEditWindows?  "Visible" test ignores Clipboard if not on-screen
  396.    -------------------------------------------------------------------}
  397. function Test ( P : PView ) : boolean ; FAR ;
  398. begin
  399.    Test                      := IsEdit ( P ) and
  400.                                 Visible ( P ) ;
  401. end ;
  402.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  403.    PROCESS
  404.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  405. begin
  406.    ExistEditor               := Desktop^.FirstThat ( @Test ) <> NIL ;
  407. end ;
  408.    {===================================================================
  409.  
  410.    EXIST - return pointer to file if in Desktop
  411.  
  412.    ===================================================================}
  413. function ExistEdName ( S : string ) : pointer ;
  414.    {-------------------------------------------------------------------
  415.    -------------------------------------------------------------------}
  416. function Test ( P : PView ) : boolean ; FAR ;
  417. begin
  418.    Test                      := FALSE ;
  419.    if not IsEDIT ( P ) then EXIT ;
  420.    with PEDITWINDOW ( P )^ do
  421.       Test                   := Editor^.FileName = S ;
  422. end ;
  423.    {- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  424.    PROCESS
  425.    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}
  426. begin
  427.    S                         := StrUpCase ( S ) ;
  428.    ExistEdName               := Desktop^.FirstThat ( @Test ) ;
  429. end ;
  430.    {|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  431.  
  432.    CURRENT
  433.  
  434.    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}
  435.    {===================================================================
  436.  
  437.    ED
  438.  
  439.    ===================================================================}
  440. function EdIsCurrent : boolean ;
  441. begin
  442.    EdIsCurrent               := IsEDIT ( Desktop^.Current ) ;
  443. end ;
  444.    {===================================================================
  445.  
  446.    NAME - EditWindow
  447.  
  448.    ===================================================================}
  449. function NameCurrent : string ;
  450. begin
  451.    NameCurrent               := GetNameEdit ( Desktop^.Current )
  452. end ;
  453.    {===================================================================
  454.  
  455.    UNTITLED
  456.  
  457.    ===================================================================}
  458. function UntitledIsCurrent : boolean ;
  459. begin
  460.    UntitledIsCurrent         := IsUntitled ( Desktop^.Current ) ;
  461. end ;
  462.    {===================================================================
  463.  
  464.    CLIP
  465.  
  466.    ===================================================================}
  467. function ClipIsCurrent : boolean ;
  468. begin
  469.    ClipIsCurrent             := IsClipboard ( Desktop^.Current ) ;
  470. end ;
  471.    {===================================================================
  472.  
  473.    DIALOG
  474.  
  475.    ===================================================================}
  476. function DialogIsCurrent : boolean ;
  477. begin
  478.    DialogIsCurrent           := TypeOf ( Desktop^.Current^ ) =
  479.                                 TypeOf ( TDialog ) ;
  480. end ;
  481.    {===================================================================
  482.  
  483.    HELP
  484.  
  485.    ===================================================================}
  486. function HelpIsCurrent : boolean ;
  487. begin
  488.    HelpIsCurrent             := ( TypeOf ( Desktop^.Current^ ) =
  489.                                   TypeOf ( THelpWindow ) )
  490.                                 or
  491.                                 ( TypeOf ( Application^.Current^ ) =
  492.                                   TypeOf ( THelpWindow ) ) ;
  493. end ;
  494.